package controller

import (
	"net/http"
	"think/common"
)

// ModelController 模型示例控制器
type ModelController struct {
	common.Controller
}

// Index 模型示例首页
func (c *ModelController) Index(w http.ResponseWriter, r *http.Request) {
	// 准备示例数据
	examples := []map[string]interface{}{
		{
			"title": "基础查询示例",
			"description": "使用Find方法查询所有用户数据",
		},
		{
			"title": "Where条件查询示例",
			"description": "使用WhereEQ等方法添加查询条件",
		},
		{
			"title": "排序查询示例",
			"description": "使用Order方法进行排序",
		},
		{
			"title": "分组查询示例",
			"description": "使用Group和Having方法进行分组查询",
		},
		{
			"title": "分页查询示例",
			"description": "使用Page方法进行分页查询",
		},
	}

	c.Success(w, map[string]interface{}{
		"message": "Model查询构建器示例",
		"examples": examples,
		"next_step": "访问 /model/example/:id 查看具体示例代码和结果",
	})
}

// Example 模型查询示例
func (c *ModelController) Example(w http.ResponseWriter, r *http.Request) {
	// 从URL中获取示例ID
	id := r.URL.Query().Get("id")
	if id == "" {
		id = "1"
	}

	// 创建一个用户模型实例
	userModel := common.NewModel("users")

	var result map[string]interface{}

	// 根据示例ID执行不同的查询示例
	switch id {
	case "1":
		// 基础查询示例
		result = map[string]interface{}{
			"title": "基础查询示例",
			"code": "userModel.Find()",
			"description": "查询users表中的所有数据",
			"result": userModel.Find(),
		}
	case "2":
		// Where条件查询示例
		result = map[string]interface{}{
			"title": "Where条件查询示例",
			"code": `userModel.WhereEQ("status", 1).WhereGTE("age", 18).Find()`,
			"description": "查询status=1且age>=18的用户",
			"result": userModel.WhereEQ("status", 1).WhereGTE("age", 18).Find(),
		}
	case "3":
		// 排序查询示例
		result = map[string]interface{}{
			"title": "排序查询示例",
			"code": `userModel.Order("created_at", "DESC").Order("id", "ASC").Find()`,
			"description": "按created_at降序和id升序排序",
			"result": userModel.Order("created_at", "DESC").Order("id", "ASC").Find(),
		}
	case "4":
		// 分组查询示例
		result = map[string]interface{}{
			"title": "分组查询示例",
			"code": `userModel.Field("gender, COUNT(*) as count").Group("gender").Having("count > 10").Find()`,
			"description": "按gender分组并统计每组数量",
			"result": userModel.Field("gender, COUNT(*) as count").Group("gender").Having("count > 10").Find(),
		}
	case "5":
		// 分页查询示例
		result = map[string]interface{}{
			"title": "分页查询示例",
			"code": `userModel.Page(1, 10).Find()`,
			"description": "查询第1页，每页10条数据",
			"result": userModel.Page(1, 10).Find(),
		}
	case "6":
		// 复杂条件查询示例
		result = map[string]interface{}{
			"title": "复杂条件查询示例",
			"code": `userModel.WhereEQ("status", 1).WhereLike("username", "admin").Order("created_at", "DESC").Page(1, 20).Find()`,
			"description": "组合使用多种查询条件",
			"result": userModel.WhereEQ("status", 1).WhereLike("username", "admin").Order("created_at", "DESC").Page(1, 20).Find(),
		}
	case "7":
		// IN条件查询示例
		result = map[string]interface{}{
			"title": "IN条件查询示例",
			"code": `userModel.WhereIN("id", []interface{}{1, 2, 3, 4, 5}).Find()`,
			"description": "查询id在指定范围内的用户",
			"result": userModel.WhereIN("id", []interface{}{1, 2, 3, 4, 5}).Find(),
		}
	case "8":
		// 统计查询示例
		result = map[string]interface{}{
			"title": "统计查询示例",
			"code": `count := userModel.WhereEQ("status", 1).Count()`,
			"description": "统计符合条件的记录数量",
			"result": map[string]int64{"count": userModel.WhereEQ("status", 1).Count()},
		}
	case "9":
		// 更新操作示例
		result = map[string]interface{}{
			"title": "更新操作示例",
			"code": `userModel.WhereEQ("id", 1).Update(map[string]interface{}{"status": 0})`,
			"description": "更新指定条件的记录",
			"result": map[string]bool{"success": userModel.WhereEQ("id", 1).Update(map[string]interface{}{"status": 0})},
		}
	case "10":
		// 插入操作示例
		result = map[string]interface{}{
			"title": "插入操作示例",
			"code": `userModel.Save(map[string]interface{}{"username": "testuser", "password": "123456", "status": 1})`,
			"description": "插入一条新记录",
			"result": map[string]bool{"success": userModel.Save(map[string]interface{}{"username": "testuser", "password": "123456", "status": 1})},
		}
	default:
		result = map[string]interface{}{
			"error": "示例不存在",
			"available_examples": 10,
		}
	}

	c.Success(w, result)
}

// GetOne 获取单条数据示例
func (c *ModelController) GetOne(w http.ResponseWriter, r *http.Request) {
	// 创建一个用户模型实例
	userModel := common.NewModel("users")

	// 获取ID参数
	id := r.URL.Query().Get("id")
	if id == "" {
		id = "1"
	}

	// 查询单条数据
	result := userModel.FindOne()

	c.Success(w, map[string]interface{}{
		"title": "查询单条数据示例",
		"code": `userModel.FindOne()`,
		"result": result,
	})
}

// Advanced 高级查询示例
func (c *ModelController) Advanced(w http.ResponseWriter, r *http.Request) {
	// 创建一个用户模型实例
	userModel := common.NewModel("users")

	// 高级查询示例 - 使用自定义Condition结构体
	condition := common.Condition{
		Field:    "status",
		Type:     common.EQ,
		Value:    1,
		Relation: "AND",
	}

	// 执行查询
	result := userModel.Where(condition).WhereGT("score", 80).Order("score", "DESC").Limit(10).Find()

	c.Success(w, map[string]interface{}{
		"title": "高级查询示例",
		"code": `condition := common.Condition{\n  Field:    "status",\n  Type:     common.EQ,\n  Value:    1,\n  Relation: "AND",\n}\nuserModel.Where(condition).WhereGT("score", 80).Order("score", "DESC").Limit(10).Find()`,
		"description": "使用自定义Condition结构体进行高级查询",
		"result": result,
	})
}